package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.PictureAdapter;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.UserPermission;
import com.sx.zongzhi.bean.WorkLogBean;
import com.sx.zongzhi.presenter.WorkLogDetailPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.WorkLogDetailView;
import com.sx.zongzhi.voiceamr.Player;
import com.sx.zongzhi.widget.CustomDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 工作日志详情
 */
public class WorkLogDetailActivity extends BaseActivity implements WorkLogDetailView {


    @BindView(R.id.tv_Name)
    TextView tv_Name;
    @BindView(R.id.tv_Time)
    TextView tv_Time;
    @BindView(R.id.tv_Title)
    TextView tv_Title;
    @BindView(R.id.tv_Content)
    TextView tv_Content;

    @BindView(R.id.recyclerView)
    RecyclerView rv_imageRV;
    @BindView(R.id.ll_content)
    LinearLayout llContent;

    private WorkLogBean workLogBean;
    private Disposable disposable;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_work_log_detail);
        setTitleName("工作日志详情");
        UserPermission userPermission = (UserPermission) getIntent().getSerializableExtra("userPermission");
        if (userPermission != null && userPermission.isDeleteSingleWorkDiary()) {
            setRightImage(R.mipmap.icon_nav_delete);
        }
        if (userPermission != null && userPermission.isUpdateWorkDiary()) {
            setRightImage2(R.mipmap.icon_nav_edit);
        }
        ButterKnife.bind(this);
        workLogBean = (WorkLogBean) getIntent().getSerializableExtra("workLogBean");
        initRx();
        initView();
    }

    private void initRx() {
        disposable = MyRxBus.get().toObservable(WorkLogBean.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<WorkLogBean>() {
                    @Override
                    public void accept(WorkLogBean bean) throws Exception {
                        workLogBean = bean;
                        initView();
                    }
                });
    }

    private void initView() {
        tv_Name.setText(BaseUtil.isEmpty(workLogBean.getWorkUserName()) ? "" : workLogBean.getWorkUserName());
        tv_Time.setText(BaseUtil.isEmpty(workLogBean.getWorkTime()) ? "" : workLogBean.getWorkTime());
        tv_Title.setText(BaseUtil.isEmpty(workLogBean.getTitle()) ? "" : workLogBean.getTitle());
        if (BaseUtil.isEmpty(workLogBean.getWorkContent())) {
            llContent.setVisibility(View.GONE);
        } else {
            llContent.setVisibility(View.VISIBLE);
            tv_Content.setText(workLogBean.getWorkContent());
        }
        if (workLogBean.getFiles() != null && workLogBean.getFiles().size() > 0) {//可能没有 看类型
            rv_imageRV.setLayoutManager(new LinearLayoutManager(this, LinearLayout.HORIZONTAL, false));
            final PictureAdapter pictureAdapter = new PictureAdapter(this, workLogBean.getFiles());
            rv_imageRV.setAdapter(pictureAdapter);
        } else {
            rv_imageRV.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.imv_Right, R.id.imv_Right2})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.imv_Right:
                createDelete();
                break;
            case R.id.imv_Right2:
                startActivity(new Intent(this, UpdateWorkLogActivity.class).putExtra("workLogBean", workLogBean));
                break;
        }
    }

    private void createDelete() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        CustomDialog dialog = builder.setMessage("确定删除此工作日志吗？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                new WorkLogDetailPest(WorkLogDetailActivity.this).deleteWorkLog(WorkLogDetailActivity.this, workLogBean.getId());
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }

    @Override
    public void getWorkLogDetailSuccess(WorkLogBean workLogBean) {
    }

    @Override
    public void deleteWorkLogSuccess(Boolean deleteResult) {
        ToastUtils.showToast(WorkLogDetailActivity.this, "删除成功");
        MyRxBus.get().post(new MsgEvent("WorkLogActivity", 1, null));
        finish();
    }

    @Override
    public void deletWorkLogFail(String message) {
        ToastUtils.showToast(WorkLogDetailActivity.this, message);
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(WorkLogDetailActivity.this, message);
    }

    @Override
    protected void onDestroy() {
        if (disposable != null) {
            disposable.dispose();
        }
        super.onDestroy();
    }
}
